﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement
{
    public class Player : DrawableGameComponent
    {
        private static int FullHealth = 100;
        private static Vector2 HealthDisplayPosition = new Vector2(800, 700);

        public Ship m_Ship;
        public int m_Health;
        private Arrow m_Arrow;

        private SpriteBatch m_kSpriteBatch;
        private SpriteFont m_kFont;

        public Player(Game game)
            : base(game)
        {
            m_Ship = new Ship(game);
            game.Components.Add(m_Ship);

            m_Arrow = new Arrow(game, this, CheckpointManager.GetCheckpointManager());
            game.Components.Add(m_Arrow);

            m_Health = Player.FullHealth;

            DrawOrder = 1000; // Draw on top of everything else.
        }

        protected override void LoadContent()
        {
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)this.Game.Services.GetService(typeof(IGraphicsDeviceService));

            m_kSpriteBatch = new SpriteBatch(graphicsService.GraphicsDevice);
            m_kFont = Game.Content.Load<SpriteFont>("fpsfont");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (!GameplayScreen.pause)
            {
                m_kSpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

                // Color this based on health amount
                Color DrawColor;
                if (m_Health > Player.FullHealth * 0.3) DrawColor = Color.Green;
                else DrawColor = Color.Red;
                m_kSpriteBatch.DrawString(m_kFont, "HP: " + m_Health.ToString(), Player.HealthDisplayPosition, DrawColor);
                m_kSpriteBatch.End();
            }
            base.Draw(gameTime);
        }

        public void TakeDamage(int amount)
        {
            m_Health -= amount;
            if (m_Health < 0)
            {
                m_Health = 0;
            }
        }

        public bool IsShipDestroyed()
        {
            return (m_Health <= 0);
        }

        // Remove player's resources.
        public void Remove()
        {
            Game.Components.Remove(m_Arrow);
            Game.Components.Remove(m_Ship);
        }
    }
}
